﻿#region Using directives

using System;
using System.ComponentModel;
using System.Reflection;
using NetROS.Tiers.Entities.Validation;

#endregion

namespace NetROS.Tiers.Entities
{	
	///<summary>
	/// An object representation of the 'tblBlagajnaStavka' table. [No description found the database]	
	///</summary>
	/// <remarks>
	/// This file is generated once and will never be overwritten.
	/// </remarks>	
	[Serializable]
	[CLSCompliant(true)]
	public partial class BlagajnaStavka : BlagajnaStavkaBase
	{		
		#region Constructors

		///<summary>
		/// Creates a new <see cref="BlagajnaStavka"/> instance.
		///</summary>
		public BlagajnaStavka():base()
		{
            //  DateTime danas = DateTime.Today;
            //base.DatumDokumenta = new DateTime(danas.Year,danas.Month,1);
		}	
                protected override void AddValidationRules()
        {
            base.AddValidationRules();
            //ValidationRules.AddRule(
            //    CommonRules.StringRequired,
            //    new ValidationRuleArgs("Opis", "Opis"));
            ValidationRules.AddRule(
            CheckIsIDNotSet,
             new ValidationRuleArgs("IDKonto", "Konto"));
            ValidationRules.AddRule(
            CheckIsIDNotSet,
             new ValidationRuleArgs("IDOrganizacija", "Mjesto troška"));
             ValidationRules.AddRule(TestDugPotreban,new ValidationRuleArgs("Ulaz", "Ulaz"));
            ValidationRules.AddRule(TestDugPotreban,new ValidationRuleArgs("Izlaz", "Izlaz"));
                    
        }

		
		#endregion

         bool CheckIsIDNotSet(object target, ValidationRuleArgs e)
        {
            bool result = true;

            PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

                int value = (int)p.GetValue(target, null);

                result = !value.Equals(0);

                if (!result)
                {
                    if (string.IsNullOrEmpty(e.Description)) e.Description = string.Format("Izaberite {0}",e.FriendlyName);
                }

                return result;
            
        }
              bool TestDugPotreban(Object sender, ValidationRuleArgs e)
        {
            BlagajnaStavka _etotest = sender as BlagajnaStavka;
            if (_etotest != null)
            {
                if (_etotest.Ulaz != 0 && _etotest.Izlaz != 0)
                {
                    e.Description = "Postavite na 0 ili Ulaz ili Izlaz";
                    return false;
                }
                if (_etotest.Ulaz == 0 && _etotest.Izlaz == 0)
                {
                    e.Description = "Unesite vrijednost u Izlaz ili Ulaz";
                    return false;
                }
            }
            return true;
        }

        [DescriptionAttribute(""), System.ComponentModel.Bindable( System.ComponentModel.BindableSupport.Yes)]
		[DataObjectField(false, false, false)]
		public virtual System.Decimal SaldoIzvjestaja
		{
			get { return Ulaz - Izlaz; }
			
			
		}
	}

}
